<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/22
 * Time: 13:58
 */
namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Api\WeChatControllerBase;
use App\Jobs\CaptchaRecordSave;
use App\Libs\TencentCloud\FaceIdentify;
use App\Modules\Auth\Services\SmsService;
use Illuminate\Http\Request;
use JWTAuth;
use JWTFactory;

use Redis;



class AuthController extends WeChatControllerBase {
    /**
     * @apiDefine Auth 授权
     */
    /**
     * @apiVersion 1.0.0
     * @apiDescription 用户授权，用token换取uid
     * @api {POST} v1/auth/get_token 获取用户授权信息
     * @apiParam   {string}  code 授权时要用到的code参数
     * @apiGroup Auth
     * @apiName index
     * @apiSuccess {string}  token   用户token
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 200,
     *         "msg": "",
     *         "data": {'token':'COVBZ-OVL6O-L4WWD-SBO4P-BVB22-LLB3U'}
     *     }
     */
    public function index(Request $request)
    {

        $code = $request->input('code','');
        $phone = $request->input('phone','');
        $type = $request->input('from_type','');

        if(empty($code)||empty($phone)) {
            return response()->error('PARAM_EMPTY');
        }
        if($phone == '18173907266' && $code=='666666') {
            //测试账号
        }else{
            if(empty(Redis::get('MSG_SENT_CODE:'.$phone)) ) {
                return response()->error('CAPTCHA_EXPIRED');
            }
            if($code != Redis::get('MSG_SENT_CODE:'.$phone)) {
                return response()->error('VERIFY_CODE_ERROR');
            }
        }
        $jg_token = $request->input('jg_token','');
        $jg_register_id = $request->input('jg_register_id','');
        //$ = $request->input('jg_token','');
        $user_agent = $request->userAgent();

        $user = \App\Modules\User\Models\User::findByPhone($phone);

        if(empty($user)) {
            $user = \App\Modules\User\Models\User::create(['phone'=>$phone,'from_type'=>$type,'jg_token'=>$jg_token,'jg_register_id'=>$jg_register_id,'user_agent'=>$user_agent,'login_device'=>$type]);
        }else{
            $user->jg_token = $jg_token;
            $user->user_agent = $user_agent;
            $user->login_device = $type;
            $user->jg_register_id = $jg_register_id;
            $user->save();
        }
        //$id = $userinfo->id;
        $expire_at = time() + 43200*60;
        $token = JWTAuth::fromUser($user);
        \Redis::set('app_login:'.$user->id,$token);
        $data = array(
            'token' => $token,
            'expire_at'=>$expire_at,
            'user_info' => $user
        );

        
        return response()->success($data);
    }

    public function send_sms(Request $request) {

        $phone = $request->input('phoneNumber');
        if(empty($phone)) {
            return response()->error('PARAM_EMPTY');
        }
        if(!SmsService::checkPhone($phone)) {
            return response()->error('INVALID_PHONE');
        }
        if(Redis::get('MSG_SENT_DONE:'.$phone) ==1) {
            return response()->error('TOO_FREQUENT');
        }
        $templateCode = 'SMS_181550126';
        $sms_service = new SmsService($templateCode);
        $res = $sms_service->sendSms($phone);

        if($res === true) {
            return response()->success();
        }
        return response()->error('SEND_SMS_FAILED');
    }

}